from collections import defaultdict

from common.mssql_helper import g_msql_inst
from common.utils import create_uuid

__author__ = "Ypf"
_date_ = "2019/7/23-10:30"
class Xstj(object):
    @staticmethod
    def get_dic_pp(sql):
        # 获取相应的对比sql查询语句
        res_list1 = g_msql_inst.fetch_all(sql)
        dic = defaultdict(dict)
        for (pp, cpjl, cpgly, xsje) in res_list1:
            if pp == '':
                pass
            else:
                dic[pp] = [cpjl, cpgly, xsje]
        return dic

    @staticmethod
    def get_dic_kh(sql):
        # 获取相应的对比sql查询语句
        res_list1 = g_msql_inst.fetch_all(sql)
        dic=defaultdict(dict)
        for (ghdw,ywy,zd,xsje) in res_list1:
            if ghdw=='':
                pass
            else:
                dic[ghdw]=[ywy,zd,xsje]
        return  dic

    @staticmethod
    def get_dic_jl(sql):
        res_list1 = g_msql_inst.fetch_all(sql)
        dic = defaultdict(dict)
        for (ywy,zd,xsje) in  res_list1:
            if ywy=='':
                pass
            else:
                dic[ywy]=[ywy,zd,xsje]
        return dic

    @staticmethod
    def insert_pp(db_insert_sql, dic, nowtime):
        for key in dic:
            id = create_uuid()
            value_list = []
            value_list.append(key)
            value_list.extend(dic[key])
            value_list.append(nowtime + '-01')
            value_list.append(id)
            value_tuple = tuple(value_list)
            excuete_sql = db_insert_sql % value_tuple
            print(excuete_sql)
            g_msql_inst.db_excuete(excuete_sql)

    @staticmethod
    def insert_kh(db_insert_sql, dic, nowtime):
        for key in dic:
            id = create_uuid()
            value_list = []
            value_list.append(key)
            value_list.extend(dic[key])
            value_list.append(nowtime + '-01')
            value_list.append(id)
            value_tuple = tuple(value_list)
            excuete_sql = db_insert_sql % value_tuple
            print(excuete_sql)
            g_msql_inst.db_excuete(excuete_sql)

    @staticmethod
    def insert_xs(db_insert_sql, dic, nowtime):
        for key in dic:
            id = create_uuid()
            dic[key].append(nowtime + '-01')
            dic[key].append(id)
            value_tuple = tuple(dic[key])
            excuete_sql = db_insert_sql % value_tuple
            print(excuete_sql)
            g_msql_inst.db_excuete(excuete_sql)